﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using HashFoo.Rest.Common;
using HashFoo.Rest.Server;
using HashFoo.Rest.ServerTests.FakeHttpStack;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HashFoo.Rest.ServerTests
{
    [TestClass]
    public class ResourceRouteHelpersTests
    {
        [TestMethod]
        public void creates_mvc_routes_from_resourceuri_collection()
        {
            const string route = "handlers/test_uri";
            var resourceUriCollection = new ResourceUriCollection(
                new[]
                {
                    new ResourceUri { ResourceType = typeof(string), Uri = route }
                });

            var routes = new RouteCollection();
            routes.RegisterResourceRoutes(resourceUriCollection);

            Assert.AreEqual(resourceUriCollection.Count(), routes.Count);
        }

        [TestMethod]
        public void find_handler_type_if_route_has_resource_route_descriptor()
        {
            var routeData = new RouteData();
            routeData.Values["shouldnt_matter"] = new ResourceRouteDescriptor { HandlerType = typeof(string) };
            var context = new RequestContext(new FakeHttpContext(), routeData);

            var handlerType = context.GetHandlerTypeFromRequest(null);

            Assert.IsNotNull(handlerType);
            Assert.AreEqual(typeof(string), handlerType);
        }

        [TestMethod]
        public void null_type_returned_if_route_has_no_resource_route_descriptor()
        {
            var routeData = new RouteData();
            var context = new RequestContext(new FakeHttpContext(), routeData);

            var handlerType = context.GetHandlerTypeFromRequest(null);

            Assert.IsNull(handlerType);
        }
    }
}
